﻿using AutoMapper;
using Infrastructure.Extend;
using Infrastructure.Web;
using MyData.Dto;
using MyData.Models;
using MyDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyApplication
{
    public class DepartApp
    {
        private IDepartRepository _service;
        public DepartApp(IDepartRepository Service)
        {
            _service = Service;
        }
        public PageDto<DepartDto> GetDeparts(QueryListModel query, string name,string orderName)
        {
            //var expression = ExtLinq.True<Depart>();
            //if (!string.IsNullOrEmpty(name))
            //{
            //    expression = expression.And(t => t.Name.Contains(name));
            //}
            //var result = _service.IQueryable(expression).OrderByDescending(t => t.CreatedTime).Paging(query.Limit, query.Offset);
            //return new PageDto<DepartDto>()
            //{
            //    total = result.total,
            //    rows = Mapper.Map<List<Depart>, IList<DepartDto>>(result.rows.ToList())
            //};

            return _service.GetDeparts(query,name,orderName);

        }
        public bool SaveDepart(Depart depart)
        {
            if (depart.Id == Guid.Empty)
            {
                depart.Id = Guid.NewGuid();
                if (_service.FindEntity(c => c.Name == depart.Name) != null)
                {
                    throw new KnownException("部门已存在");
                }
                return _service.Insert(depart) > 0;
            }
            else
            {
                var entity = _service.FindEntity(depart.Id);
                entity.Name = depart.Name;
                entity.Remark = depart.Remark;
                entity.OrderNum = depart.OrderNum;
                entity.IsShow = depart.IsShow;

                return _service.Update(entity) > 0;
            }


        }

        public DepartDto GetDepart(Guid Id)
        {
            var entity = _service.FindEntity(Id);
            return Mapper.Map<Depart, DepartDto>(entity);
        }
        public Guid? GetDepartByName(string name)
        {
            var entity = _service.FindEntity(c=>c.Name== name);
            return entity?.Id;
        }
        public bool DepartDelete(Guid Id)
        {
            if (Id == Guid.Empty)
                throw new KnownException("访客部请误删");
            var entity = _service.FindEntity(Id);
            return _service.Delete(entity)>0;
        }
        public List<DepartDto> GetList()
        {
            var result = _service.IQueryable().Where(c=>c.Id!=Guid.Empty).OrderByDescending(c=>c.OrderNum).ToList();
            return Mapper.Map<List<Depart>, List<DepartDto>>(result);
        }
        public List<DepartDto> GetShowList()
        {
            var result = _service.IQueryable().Where(c => c.Id != Guid.Empty&&c.IsShow==0).OrderByDescending(c => c.OrderNum).ToList();
            return Mapper.Map<List<Depart>, List<DepartDto>>(result);
        }
        public List<string> GetDepartIn(Guid Id)
        {
            return _service.GetDepartIn(Id);
        }
    }
}
